package net.openl10n.packaging.project;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import net.openl10n.api.project.Project;
import net.openl10n.opc4j.Package;
import net.openl10n.opc4j.PackageAccess;
import net.openl10n.opc4j.exceptions.InvalidFormatException;
import net.openl10n.packaging.CoreFilePropertiesPart;
import net.openl10n.packaging.OpenLocPackage;
import net.openl10n.packaging.OpenLocPartFactory;
import net.openl10n.packaging.OpenLocPartFactoryManager;
import net.openl10n.packaging.document.ContentPart;
import net.openl10n.packaging.document.ContentTargetPart;
import net.openl10n.packaging.document.DocumentPart;
import net.openl10n.packaging.document.comments.SimpleCommentsPart;

public class ProjectPackage extends OpenLocPackage{
	
	private static final OpenLocPartFactoryManager partFactoryManager = new OpenLocPartFactoryManager();
	
	static{
		partFactoryManager.add(CoreFilePropertiesPart.FACTORY);
		partFactoryManager.add(ProjectPart.FACTORY);
		partFactoryManager.add(DocumentPart.FACTORY);
		partFactoryManager.add(ContentPart.FACTORY);
		partFactoryManager.add(SimpleCommentsPart.FACTORY);
		partFactoryManager.setPrefix(DocumentPart.CONTENT_TYPE, "/documents");
		partFactoryManager.add(ContentTargetPart.FACTORY);
	}

	public static void registerPartFactory(OpenLocPartFactory<?> factory){
		partFactoryManager.add(factory);
	}
	
	protected ProjectPackage(Package pack, OpenLocPartFactoryManager locPartFactoryManager) {
		super(pack,locPartFactoryManager);
	}
	
	public ProjectPart addProjectPart(){
		try{
			ProjectPart projectPart = addNewPart(ProjectPart.class);
			return projectPart;
		}
		catch(InvalidFormatException e){
			throw new RuntimeException(e);
		}
	}
	public ProjectPart addProjectPart(String id, String name){
		return addProjectPart(id, name, null);
	}	
	public ProjectPart addProjectPart(String id, String name, String summary){
		try{
			ProjectPart projectPart = addNewPart(ProjectPart.class);
			Project p = projectPart.getProject();
			p.setId(id);
			p.setName(name);
			p.setSummary(summary);
			return projectPart;
		}
		catch(InvalidFormatException e){
			throw new RuntimeException(e);
		}
	}
	
	public ProjectPart getProjectPart(){
		return getPartOfType(ProjectPart.class);
	}
	
	public static ProjectPackage open(Package pack){
		return new ProjectPackage(pack, partFactoryManager);
	}
	
	public static ProjectPackage open(File file) throws InvalidFormatException{
		Package pack = Package.open(file.getAbsolutePath());
		return open(pack);
	}

	public static ProjectPackage open(File file, PackageAccess access) throws InvalidFormatException{
		Package pack = Package.open(file.getAbsolutePath(), access);
		return open(pack);
	}

	public static ProjectPackage open(InputStream stream) throws InvalidFormatException, IOException{
		Package pack = Package.open(stream);
		return open(pack);
	}

	public static ProjectPackage create(Package pack){
		return new ProjectPackage(pack, partFactoryManager);
	}
	
	public static ProjectPackage create(File file) throws InvalidFormatException{
		Package pack = Package.create(file);
		return create(pack);
	}

	public static ProjectPackage create(OutputStream stream) throws InvalidFormatException, IOException{
		Package pack = Package.create(stream);
		return create(pack);
	}

}
